package com.app.mingluxing.location;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

import de.greenrobot.event.EventBus;


/**
 * 添加sn码
 * Created by Administrator on 2015/10/21.
 */
public class SnCodeActivity extends BaseActivity {

    private EditText et_sn;
    private TextView tv_done;
    private ImageView iv_left;
    private ImageView iv_delete_sn;
//    private TextView tv_watch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sn_code);
        initView();

    }

    private void initView() {

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("输入SN码");

        et_sn = (EditText) findViewById(R.id.et_sn);
        tv_done = (TextView) findViewById(R.id.tv_done);
//        tv_watch = (TextView) findViewById(R.id.tv_watch);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_delete_sn = (ImageView) findViewById(R.id.iv_delete_sn);

        iv_left.setOnClickListener(this);
        iv_delete_sn.setOnClickListener(this);
        tv_done.setOnClickListener(this);

        et_sn.setText("OVENR9");
        et_sn.setSelection(et_sn.getText().toString().length());

        et_sn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_sn.setVisibility(et_sn.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);
                    et_sn.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_sn.setVisibility(et_sn.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });

                } else {
                    iv_delete_sn.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();

                break;
            case R.id.iv_delete_sn:

                et_sn.setText("");

                break;
            case R.id.tv_done:

                if (CommonUtils.isNull(et_sn.getText().toString().trim())) {
                    showToast("请输入手表SN码!");
                    return;
                }
                getSnCodeData();

                break;
        }
    }

    private void getSnCodeData() {

        JSONObject j = new JSONObject();
        j.put("id", sp.getString(Constant.CURRENTCHILDID, ""));
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("sn", et_sn.getText().toString().trim());

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.ADDWATCH,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                showToast("绑定成功");

                                //通知定位页面绑定了宝宝
                                EventBus.getDefault().post(true);

                                finish();

                            } else//弹出错误提示

                                showToast(Constant.showErrorMsg(result.getInt("code"), SnCodeActivity.this));

                        } catch (Exception e) {

                            e.printStackTrace();

                        }
                    }

                }, null);

    }
}
